package pers.luchuan.rest.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.luchuan.rest.domain.User;
import pers.luchuan.rest.util.JSONUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("testGet1")
    public ResponseEntity testGet1() {
        ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://localhost/getUser?name={1}", User.class, "zhangsan");
        return responseEntity;
    }

    @GetMapping("testGet2")
    public ResponseEntity testGet2() {
        Map<String, Object> map = new HashMap<>();
        map.put("name","zhangsan");
        ResponseEntity<User> forEntity = restTemplate.getForEntity("http://localhost/getUser?name={name}", User.class, map);
        return forEntity;
    }

    /**
     * getForObject函数实际上是对getForEntity函数的进一步封装，如果你只关注返回的消息体的内容，对其他信息都不关注，此时可以使用getForObject
     * @return
     */
    @GetMapping("testGet3")
    public User testGet3() {
        Map<String, Object> map = new HashMap<>();
        map.put("name","zhangsan");
        User user = restTemplate.getForObject("http://localhost/getUser?name={name}", User.class, map);
        return user;
    }

    @GetMapping("testPost")
    public ResponseEntity testPost() {
        MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
        valueMap.add("name","zhangsan");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(valueMap, headers);
        ResponseEntity<User> forEntity = restTemplate.postForEntity("http://localhost/postUser", entity, User.class);
        return forEntity;
    }

    @GetMapping("testPost1")
    public ResponseEntity testPost1() {
        MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
        valueMap.add("name","zhangsan");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(valueMap, headers);
        ResponseEntity<User> forEntity = restTemplate.postForEntity("http://localhost/postUser2", entity, User.class);
        return forEntity;
    }

    @GetMapping("testPost2")
    public ResponseEntity testPost2() {
        User user = new User(18, "zhangsan");
        String json = JSONUtil.toJSONString(user);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        ResponseEntity<User> forEntity = restTemplate.postForEntity("http://localhost/postUser3", entity, User.class);
        return forEntity;
    }

    @GetMapping("testPut")
    public void testPut() {
        restTemplate.put("http://localhost/put/{1}", "","zhangsan");
    }

    @GetMapping("testDelete")
    public void testDelete() {
        restTemplate.delete("http://localhost/delete/{1}", "zhangsan");
    }
}
